package com.yorick.authority.controller;

import com.yorick.authority.enums.Account;
import com.yorick.authority.modal.request.role.*;
import com.yorick.authority.service.RoleService;
import com.yorick.common.Result.SimpleFactory.ResultFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

/**
 * @program: vr_manage
 * @description: 角色管理
 * @author: Yorick
 * @create: 2022-05-22 18:36
 **/
@Slf4j
@Controller
@RequestMapping("role")
public class RoleController {
    @Resource
    RoleService roleService;

    @RequestMapping(value = "roleList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> roleList(@Valid @RequestBody RoleListRequest requestBody,
                                        HttpServletRequest request) {
        HttpSession session = request.getSession();
        requestBody.setAid(Integer.parseInt(session.getAttribute(Account.aid.name()).toString()));
        requestBody.setSuperRole(Boolean.parseBoolean(session.getAttribute(Account.superRole.name()).toString()));
        return ResultFactory.success(roleService.roleList(requestBody)).display();

    }

    @RequestMapping(value = "addRole", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> addRole(@Valid @RequestBody RoleAddRequest requestBody,
                                       HttpServletRequest request) {
        HttpSession session = request.getSession();
        requestBody.setAid(Integer.parseInt(session.getAttribute(Account.aid.name()).toString()));
        requestBody.setSuperRole(Boolean.parseBoolean(session.getAttribute(Account.superRole.name()).toString()));
        return roleService.addRole(requestBody);
    }

    @RequestMapping(value = "editRole", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> editRole(@Valid @RequestBody RoleEditRequest requestBody,
                                        HttpServletRequest request) {
        HttpSession session = request.getSession();
        requestBody.setAid(Integer.parseInt(session.getAttribute(Account.aid.name()).toString()));
        return roleService.editRole(requestBody);
    }

    @RequestMapping(value = "roleDetail", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> roleDetail(@Valid @RequestBody RoleDetailRequest requestBody) {
        return ResultFactory.success(roleService.roleDetail(requestBody)).display();
    }

    @RequestMapping("deleteRole")
    @ResponseBody
    public Map<String, Object> deleteRole(@Valid @RequestBody RoleDeleteRequest requestBody) {
        return roleService.deleteRole(requestBody);
    }
}
